package erfgame.core.world.terrain;

import java.awt.Point;
import java.util.Comparator;


import erfgame.core.Point3D;
import erfgame.core.world.World;
import erfgame.core.world.WorldContext;

public class GridUtils {
	
//	public static final Point getGridCoordinate( World world, int terrainX, int terrainY, Point result ) {
//		int gridDiagonal = world.getGridDiagonal();
//		int gridDiagonalOn2 = gridDiagonal/2;
//		int gridDiagonalMask = world.getGridDiagonalMask();
//		int gridDiagonalShift = world.getGridDiagonalShift()-1;
//		
//		int lgx = terrainX & gridDiagonalMask;
//		int lgy = terrainY & gridDiagonalMask;
//		
//		int gx = terrainX >> gridDiagonalShift;
//		int gy = terrainY >> gridDiagonalShift;
//		
//		int ldx = gridDiagonalOn2 - lgx;
//		int ldy = gridDiagonalOn2 - lgy;
//
//		if( ldx > ldy ) {
//			if( gridDiagonal - ldx > ldy ) {
//				// do nothing
//			} else {
//				gx++;
//				gy++;
//			}
//		} else {
//			if( gridDiagonal - ldx > ldy ) {
//				gy++;
//			} else {
//				gy+=2;
//			}
//		}
//		
//		result.x = gx;
//		result.y = gy;
//		return result;
//	}
	
	public static final Comparator<Point> createGridPointComparator( final World world ) {
		return new Comparator<Point>() {

			int ymult = world.getBlocksX() * world.getXGridTilesPerBlock();
			
			public int compare(Point o1, Point o2) {
				return (o2.y * ymult + o2.x) - (o1.y * ymult + o1.x); 
			}
		};
	}
	
	public static final Comparator<Point3D> createGridPoint3DComparator( final WorldContext worldContext ) {
		return new Comparator<Point3D>() {
			public int compare(Point3D o1, Point3D o2) {
				World world = worldContext.getCurrentWorld();
				int ymult = world.getBlocksZ();
				int xmult = world.getBlocksY() * world.getYGridTilesPerBlock() * ymult;
				return (o2.z + o2.y * ymult + o2.x * xmult ) - (o1.z + o1.y * ymult + o1.x * xmult); 
			}
		};
	}
}
